package org.finesys.quartz.controller;

import java.util.List;

import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.module.R;
import org.finesys.common.excel.annotaion.ResponseExcel;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.finesys.quartz.entity.SysJob;
import org.finesys.quartz.service.SysJobService;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 定时任务管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/job")
@Tag(description = "job", name = "定时任务")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Slf4j
public class SysJobController {

    private final SysJobService sysJobService;

    /**
     * 查询定时任务数据
     */
    @PostMapping(RestApiConfig.PAGE)
    public R<?> findPage(Page<SysJob> page, SysJob sysJob) {
        return R.success(sysJobService.findPage(page, sysJob));
    }

    /**
     * 查询定时任务数据
     */
    @PostMapping(RestApiConfig.LIST)
    public R<?> findList(SysJob sysJob) {
        return R.success(sysJobService.findList(sysJob));
    }

    /**
     * 新增定时任务
     */
    @GetMapping(RestApiConfig.GET + "/{jobId}")
    public R<?> findById(@PathVariable Long jobId) {
        return R.success(sysJobService.getById(jobId));
    }

    /**
     * 新增定时任务
     */
    @LogAnnotation(title = "新增定时任务", operateType = OperateType.INSERT)
    @PostMapping(RestApiConfig.ADD)
    @Operation(description = "新增定时任务")
    @PreAuthorize("@pms.hasPermission('sys_job_add')")
    public R<?> add(@RequestBody SysJob sysJob) {
        return sysJobService.addJob(sysJob);
    }

    /**
     * 更新定时任务
     */
    @LogAnnotation(title = "修改定时任务", operateType = OperateType.UPDATE)
    @PutMapping(RestApiConfig.UPDATE)
    @Operation(description = "修改定时任务")
    @PreAuthorize("@pms.hasPermission('sys_job_update')")
    public R<?> update(@RequestBody SysJob sysJob) {
        return sysJobService.updateJob(sysJob);
    }

    /**
     * 删除定时任务
     */
    @LogAnnotation(title = "删除定时任务", operateType = OperateType.DELETE)
    @DeleteMapping(RestApiConfig.DELETE)
    @Operation(description = "删除定时任务")
    @PreAuthorize("@pms.hasPermission('sys_job_delete')")
    public R<?> delete(@RequestBody Long[] jobIds) {
        return sysJobService.deleteJob(jobIds);
    }

    /**
     * 启动全部定时任务
     *
     * @return
     */
    @LogAnnotation(title = "启动全部暂停的定时任务", operateType = OperateType.OTHER)
    @PostMapping("/startAll")
    @PreAuthorize("@pms.hasPermission('sys_job_startAll')")
    @Operation(description = "启动全部暂停的定时任务")
    public R<?> startAll() {
        return sysJobService.startAll();
    }

    /**
     * 暂停全部定时任务
     *
     * @return
     */
    @LogAnnotation(title = "暂停全部的定时任务", operateType = OperateType.OTHER)
    @PostMapping("/stopAll")
    @PreAuthorize("@pms.hasPermission('sys_job_stopAll')")
    @Operation(description = "暂停全部的定时任务")
    public R<?> stopAll() {
        return sysJobService.stopAll();
    }

    /**
     * 启动指定的定时任务
     *
     * @return
     */
    @LogAnnotation(title = "启动指定的定时任务", operateType = OperateType.OTHER)
    @PostMapping("/start")
    @PreAuthorize("@pms.hasPermission('sys_job_start')")
    @Operation(description = "启动指定的定时任务")
    public R<?> startAllJobs(@RequestBody Long[] jobIds) {
        return sysJobService.start(jobIds);
    }

    /**
     * 启动指定的定时任务
     *
     * @return
     */
    @LogAnnotation(title = "暂停指定的定时任务", operateType = OperateType.OTHER)
    @PostMapping("/stop")
    @PreAuthorize("@pms.hasPermission('sys_job_stop')")
    @Operation(description = "暂停指定的定时任务")
    public R<?> stopAllJobs(@RequestBody Long[] jobIds) {
        return sysJobService.stop(jobIds);
    }

    /**
     * 刷新定时任务
     *
     * @return
     */
    @LogAnnotation(title = "刷新定时任务", operateType = OperateType.OTHER)
    @PostMapping("/refresh")
    @PreAuthorize("@pms.hasPermission('sys_job_refresh')")
    @Operation(description = "刷新定时任务")
    public R<?> refreshJob() {
        return sysJobService.refreshJob();
    }

    /**
     * 立刻执行定时任务
     *
     * @return
     */
    @LogAnnotation(title = "立刻执行定时任务", operateType = OperateType.OTHER)
    @PostMapping("/run")
    @PreAuthorize("@pms.hasPermission('sys_job_run')")
    @Operation(description = "立刻执行定时任务")
    public R<?> runJob(@RequestBody Long[] jobIds) {
        return sysJobService.run(jobIds);
    }


    /**
     * 检验任务名称和任务组联合是否唯一
     *
     * @return
     */
    @GetMapping("/validTaskName")
    @Operation(description = "检验任务名称和任务组联合是否唯一")
    public R<?> isValidTaskName(@RequestParam String jobName, @RequestParam String jobGroup) {
        return this.sysJobService
                .count(Wrappers.query(SysJob.builder().jobName(jobName).jobGroup(jobGroup).build())) > 0
                ? R.failed("任务重复，请检查此组内是否已包含同名任务") : R.success();
    }

    /**
     * 导出任务
     *
     * @param sysJob
     * @return
     */
    @ResponseExcel
    @GetMapping(RestApiConfig.EXPORT_EXCEL)
    @Operation(description = "导出任务")
    @PreAuthorize("@pms.hasPermission('sys_job_export')")
    public List<SysJob> export(SysJob sysJob) {
        return sysJobService.list(Wrappers.query(sysJob));
    }

}
